<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>调用一个 header 函数</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.header-register-callback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">header_register_callback</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0, PHP 7)</p><p class="refpurpose"><span class="refname">header_register_callback</span> &mdash; <span class="dc-title">调用一个 header 函数</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.header-register-callback-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>header_register_callback</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   注册一个函数，在 PHP 开始发送输出时调用。
  </p>
  <p class="para">
   PHP 准备好所有响应头，在发送内容之前执行 <code class="parameter">callback</code>，创建了一个发送响应头的操作窗口。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.header-register-callback-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<code class="parameter">callback</code></dt>

    <dd>

     <p class="para">
      在头发送前调用函数。
      它没有参数，返回的值也会被忽略。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.header-register-callback-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.header-register-callback-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-5490">
   <p><strong>Example #1 <span class="function"><strong>header_register_callback()</strong></span> 例子</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type:&nbsp;text/plain'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'X-Test:&nbsp;foo'</span><span style="color: #007700">);<br /><br />function&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">()&nbsp;{<br />&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">headers_list</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$header</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$header</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'X-Powered-By:'</span><span style="color: #007700">)&nbsp;!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header_remove</span><span style="color: #007700">(</span><span style="color: #DD0000">'X-Powered-By'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header_remove</span><span style="color: #007700">(</span><span style="color: #DD0000">'X-Test'</span><span style="color: #007700">);<br />&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">header_register_callback</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"a"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Content-Type: text/plain

a
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-function.header-register-callback-notes">
  <h3 class="title">注释</h3>
  <p class="para">
   <span class="function"><strong>header_register_callback()</strong></span> 是在头即将发送前执行的，
   所以本函数的任意内容输出都会打断输出过程。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
<p class="para">
 数据头只会在SAPI支持时得到处理和输出。
</p>
</p></blockquote>

 </div>


 <div class="refsect1 seealso" id="refsect1-function.header-register-callback-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="headers_list.html" class="function" rel="rdfs-seeAlso">headers_list()</a> - 返回已发送的 HTTP 响应头（或准备发送的）</span></li>
   <li class="member"><span class="function"><a href="header_remove.html" class="function" rel="rdfs-seeAlso">header_remove()</a> - 删除之前设置的 HTTP 头</span></li>
   <li class="member"><span class="function"><a href="header.html" class="function" rel="rdfs-seeAlso">header()</a> - 发送原生 HTTP 头</span></li>
  </ul>
 </div>


</div></div></div></body></html>